//
//  GetOrderDetailViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/31.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "GetOrderDetailViewController.h"
#import "MapDetailViewController.h"//地图详情Controller
/**百度地图*/
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import "MyAnnotationView.h"


@interface GetOrderDetailViewController () <BMKMapViewDelegate,BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate>
/**订单标题*/
@property (weak, nonatomic) IBOutlet UILabel *orderTitle;
/**取送地址*/
@property (weak, nonatomic) IBOutlet UILabel *takeAddress;
@property (weak, nonatomic) IBOutlet UILabel *deliveryAddress;
/**抢单距离*/
@property (weak, nonatomic) IBOutlet UILabel *distance;
/**待取货 待配送 距离目的地*/
@property (weak, nonatomic) IBOutlet UILabel *distanceToDestination;

/**配送费*/
@property (weak, nonatomic) IBOutlet UILabel *money;
/**派单人*/
@property (weak, nonatomic) IBOutlet UILabel *publisherName;
@property (weak, nonatomic) IBOutlet UILabel *publisherTime;
@property (weak, nonatomic) IBOutlet UIButton *callPublisher;
/**收货人*/
@property (weak, nonatomic) IBOutlet UILabel *consigneeName;
@property (weak, nonatomic) IBOutlet UILabel *consigneeTime;
@property (weak, nonatomic) IBOutlet UIButton *callConsignee;

//抢单
@property (weak, nonatomic) IBOutlet UIButton *getOrderBtn;//立即抢单
//待取货
@property (weak, nonatomic) IBOutlet UIView *takeView;
@property (weak, nonatomic) IBOutlet UIButton *confirmTakeBtn;//确认取货
@property (weak, nonatomic) IBOutlet UIButton *cancleOrder;//取消订单
//待配送
@property (weak, nonatomic) IBOutlet UIButton *confirmDeliveryBtn;//确认送达
//已送达
@property (weak, nonatomic) IBOutlet UIButton *hasBeenDeliveryBtn;//已送达


/**地图*/
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

@end

@implementation GetOrderDetailViewController
{
    BMKLocationService *_locationService;
    BMKPointAnnotation *_takeAnnotation;
    BMKPointAnnotation *_deliveryAnnotation;
    MyAnnotationView *_takeAnnoView;
    MyAnnotationView *_deliveryAnnoView;
    BMKGeoCodeSearch *_searcher;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    switch (self.state) {
        case 0:
            [self getOrderSetting];
            break;
        case 1:
            [self takeSetting];
            break;
        case 2:
            [self deliverySetting];
            break;
        case 3:
            [self hasBeenDeliveredSetting];
            break;
        default:
            break;
    }
    
    _callPublisher.layer.borderWidth = 0.5;
    _callConsignee.layer.borderWidth = 0.5;
    
    
    //地图
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;
    _mapView.showsUserLocation = YES;
    _mapView.zoomLevel = 14;
    _mapView.delegate = self;
    _mapView.mapType = BMKMapTypeStandard;
    _mapView.overlookEnabled = NO;
    _mapView.rotateEnabled = NO;
    _mapView.gesturesEnabled = NO;
    //定位
    _locationService = [[BMKLocationService alloc]init];
    _locationService.delegate = self;
    [_locationService startUserLocationService];
    
    
    //初始化检索对象
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    
    //取货地址
    _takeAnnotation = [[BMKPointAnnotation alloc] init];
    _takeAnnotation.coordinate = CLLocationCoordinate2DMake(30.304065, 120.115655);
    [_mapView addAnnotation:_takeAnnotation];
    //送货地址
    _deliveryAnnotation = [[BMKPointAnnotation alloc] init];
    _deliveryAnnotation.coordinate = CLLocationCoordinate2DMake(30.274336, 120.141555);
    [_mapView addAnnotation:_deliveryAnnotation];
    
}


#pragma mark === BaiduSDKDelegate ===
#pragma mark === 处理方向变更信息 ===
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
}
#pragma  mark === 处理位置坐标更新 ===
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    CLLocationCoordinate2D user_coordinate = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    //设置中心点
    //更新数据
    [_mapView updateLocationData:userLocation];
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = (user_coordinate.latitude + _deliveryAnnotation.coordinate.latitude + _takeAnnotation.coordinate.latitude) / 3;
    coordinate.longitude = (user_coordinate.longitude + _deliveryAnnotation.coordinate.longitude + _takeAnnotation.coordinate.longitude) / 3;
    _mapView.centerCoordinate = coordinate;
    [_locationService stopUserLocationService];
}
#pragma mark === 反向地理编码检索 ===
- (void)getAnnotationAddressWithCoordinate:(CLLocationCoordinate2D)coordinate{
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[BMKReverseGeoCodeOption alloc] init];
    reverseGeoCodeSearchOption.reverseGeoPoint = coordinate;
    BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
    reverseGeoCodeSearchOption = nil;
    if (flag)
    {
        NSLog(@"反geo检索发送成功");
        
    }
    else
    {
        NSLog(@"反geo检索发送失败");
        [SVProgressHUD dismiss];
    }
}
#pragma mark === 反向地理检索回调 ===
- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error;{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        //        _address.text = result.address;
        NSLog(@"%@", result.address);
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
    [SVProgressHUD dismiss];
}

#pragma mark === 大头针 ===
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        if (!_takeAnnoView) {
            // 创建大头针
            _takeAnnoView = [MyAnnotationView annotationViewWithMap:mapView];
            // 设置模型
            _takeAnnoView.myImage = [UIImage imageNamed:@"map-q"];
            _takeAnnoView.annotation = _takeAnnotation;
            return _takeAnnoView;
        }else{
            _deliveryAnnoView = [MyAnnotationView annotationViewWithMap:_mapView];
            // 设置模型
            _deliveryAnnoView.myImage = [UIImage imageNamed:@"map-s"];
            _deliveryAnnoView.annotation = _deliveryAnnotation;
            return _deliveryAnnoView;
        }
    }
    return nil;
}

- (IBAction)mapDetail:(id)sender {
    MapDetailViewController *map = [[MapDetailViewController alloc] init];
    map.takeCoordinate = _takeAnnotation.coordinate;
    map.deliveryCoordinate = _deliveryAnnotation.coordinate;
    [self.navigationController pushViewController:map animated:YES];
}


#pragma mark === 抢单 ===
- (void)getOrderSetting{
    _distance.hidden = NO;
    _distanceToDestination.hidden = YES;
    
    _getOrderBtn.hidden = NO;
    _takeView.hidden = YES;
    _confirmDeliveryBtn.hidden = YES;
    _hasBeenDeliveryBtn.hidden = YES;
    
    _callPublisher.layer.borderColor = RGBCOLOR(191, 197, 201).CGColor;
    [_callPublisher setTitleColor:RGBCOLOR(191, 197, 201) forState:UIControlStateNormal];
    _callPublisher.userInteractionEnabled = NO;
    _callConsignee.layer.borderColor = RGBCOLOR(191, 197, 201).CGColor;
    [_callConsignee setTitleColor:RGBCOLOR(191, 197, 201) forState:UIControlStateNormal];
    _callConsignee.userInteractionEnabled = NO;
}
/**立即抢单*/
- (IBAction)getOrder:(id)sender {
    [SVProgressHUD show];
    [SVProgressHUD showSuccessWithStatus:@"抢单成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self takeSetting];
    });
}

#pragma mark === 待取货 ===
- (void)takeSetting{
    _distance.hidden = YES;
    _distanceToDestination.hidden = NO;
    _distanceToDestination.text = @"距离取货目的地1.2km";
    
    _getOrderBtn.hidden = YES;
    _takeView.hidden = NO;
    _confirmDeliveryBtn.hidden = YES;
    _hasBeenDeliveryBtn.hidden = YES;
    
    _callPublisher.layer.borderColor = APPCOLOR.CGColor;
    [_callPublisher setTitleColor:APPCOLOR forState:UIControlStateNormal];
    _callPublisher.userInteractionEnabled = YES;
    _callConsignee.layer.borderColor = RGBCOLOR(253, 151, 52).CGColor;
    [_callConsignee setTitleColor:RGBCOLOR(253, 151, 52) forState:UIControlStateNormal];
    _callConsignee.userInteractionEnabled = YES;
}
/**确认取货*/
- (IBAction)confirmTakeGood:(id)sender {
    [SVProgressHUD show];
    [SVProgressHUD showSuccessWithStatus:@"已确认收货"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self deliverySetting];
    });
}
/**取消订单*/
- (IBAction)cancleOrder:(id)sender {
    [SVProgressHUD show];
    [SVProgressHUD showSuccessWithStatus:@"订单已取消"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self getOrderSetting];
    });
}

#pragma mark === 待配送 ===
- (void)deliverySetting{
    _distance.hidden = YES;
    _distanceToDestination.hidden = NO;
    _distanceToDestination.text = @"距离送货目的地1.2km";
    
    _getOrderBtn.hidden = YES;
    _takeView.hidden = YES;
    _confirmDeliveryBtn.hidden = NO;
    _hasBeenDeliveryBtn.hidden = YES;
    
    _callPublisher.layer.borderColor = APPCOLOR.CGColor;
    [_callPublisher setTitleColor:APPCOLOR forState:UIControlStateNormal];
    _callPublisher.userInteractionEnabled = YES;
    _callConsignee.layer.borderColor = RGBCOLOR(253, 151, 52).CGColor;
    [_callConsignee setTitleColor:RGBCOLOR(253, 151, 52) forState:UIControlStateNormal];
    _callConsignee.userInteractionEnabled = YES;
}
/**确认送达*/
- (IBAction)confirmDelivery:(id)sender {
    [SVProgressHUD show];
    [SVProgressHUD showSuccessWithStatus:@"已确认送达"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self hasBeenDeliveredSetting];
    });
}

#pragma mark === 已送达 ===
- (void)hasBeenDeliveredSetting{
    _distance.hidden = YES;
    _distanceToDestination.hidden = NO;
    _distanceToDestination.text = @"本次配送已完成";
    
    _getOrderBtn.hidden = YES;
    _takeView.hidden = YES;
    _confirmDeliveryBtn.hidden = YES;
    _hasBeenDeliveryBtn.hidden = NO;
    
    _callPublisher.layer.borderColor = APPCOLOR.CGColor;
    [_callPublisher setTitleColor:APPCOLOR forState:UIControlStateNormal];
    _callPublisher.userInteractionEnabled = YES;
    _callConsignee.layer.borderColor = RGBCOLOR(253, 151, 52).CGColor;
    [_callConsignee setTitleColor:RGBCOLOR(253, 151, 52) forState:UIControlStateNormal];
    _callConsignee.userInteractionEnabled = YES;
}

//- (void)back{
//    [self.navigationController popViewControllerAnimated:YES];
//}
- (IBAction)back:(id)sender {
    [_locationService stopUserLocationService];
    _mapView.showsUserLocation = NO;
    _locationService.delegate = nil;
    _mapView.delegate = nil;
    _searcher.delegate = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
